# Contributor: Robert T. McGibbon <rmcgibbo@gmail.com>

_realname=netcdf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.8.1
pkgrel=1
pkgdesc="Interface for scientific data access to large binary data (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('custom')
url="https://www.unidata.ucar.edu/software/netcdf/"
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-hdf5")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-cc")
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/Unidata/netcdf-c/archive/v${pkgver}.tar.gz"
        0001-mingw-cmake.patch
        0003-ncconfigure-strlcat-export.patch
        0006-cvt-conversion.patch
        0007-mingw-config.patch
        0009-mingw-getopt.patch)
sha256sums=('bc018cc30d5da402622bf76462480664c6668b55eb16ba205a0dfb8647161dd0'
            '298bfdad7fb9f3f8c17ebd1fec71d875c33f06e4f8a1e539d7cb0888ca71352d'
            '0116525b0b513b91c22519c38d9010a4eff0a7319cc2a3d882b296389119b632'
            'f5f5291c2486e9e765a93975d99871cee584bdd3d64e16745982aadeb799f00c'
            '3da38cb9beb1b075a407f698cb9ce5af14c67c1a9d1e75a70e73eec349d353c8'
            '476b49818daf6e81d6a4102749c8484f7efc4f76ba6ca1fb53aedb17749579ea')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-c-${pkgver}"

  apply_patch_with_msg \
    0001-mingw-cmake.patch \
    0003-ncconfigure-strlcat-export.patch \
    0006-cvt-conversion.patch \
    0007-mingw-config.patch \
    0009-mingw-getopt.patch
}

build() {
  [[ -d ${srcdir}/build-static-${MSYSTEM} ]] && rm -rf ${srcdir}/build-static-${MSYSTEM}
  mkdir -p ${srcdir}/build-static-${MSYSTEM} && cd ${srcdir}/build-static-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=OFF \
    -DENABLE_DLL=OFF \
    -DENABLE_DAP=ON \
    -DENABLE_TESTS=OFF \
    -DENABLE_NETCDF_4=ON \
    -Wno-dev \
    "${srcdir}/${_realname}-c-${pkgver}"
  cmake --build .

  [[ -d ${srcdir}/build-shared-${MSYSTEM} ]] && rm -rf ${srcdir}/build-shared-${MSYSTEM}
  mkdir -p ${srcdir}/build-shared-${MSYSTEM} && cd ${srcdir}/build-shared-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DENABLE_DAP=ON \
    -DENABLE_TESTS=OFF \
    -DENABLE_NETCDF_4=ON \
    -DENABLE_LOGGING=ON \
    -Wno-dev \
    "${srcdir}/${_realname}-c-${pkgver}"
  cmake --build .
}

package() {
  DESTDIR=${pkgdir} cmake --install build-static-${MSYSTEM}

  DESTDIR=${pkgdir} cmake --install build-shared-${MSYSTEM}

  install -Dm644 ${srcdir}/${_realname}-c-${pkgver}/COPYRIGHT \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYRIGHT

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/netCDF/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done

  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/bin/nc-config"
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/lib/libnetcdf.settings"
}
